package com.moneywelcome.orderassembly.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moneywelcome.annnotation.RedisAnnotation;
import com.moneywelcome.orderassembly.model.UserFinancial;
import com.moneywelcome.orderassembly.service.ProfitDayService;
import com.moneywelcome.orderassembly.service.UserFinancialService;
import com.moneywelcome.orderfacade.FinancialApi;
import com.moneywelcome.orderfacade.dto.ProfitDayDto;
import com.moneywelcome.orderfacade.dto.UserFinancialDto;
import com.moneywelcome.orderfacade.vo.OrderPageVo;
import com.moneywelcome.orderfacade.vo.ProfitDayVo;
import com.moneywelcome.orderfacade.vo.UserFinancialVo;
import com.moneywelcome.results.PageResult;
import com.moneywelcome.results.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@Slf4j
@Api(tags = "用户持仓服务")
public class FinancialApiImpl implements FinancialApi {
    @Autowired
    private UserFinancialService ufs;
    @Autowired
    private ProfitDayService pds;

    @Override
    @RedisAnnotation
    public Result<UserFinancialDto> queryById(Long financialId) {
        return Result.success(BeanUtil.copyProperties(ufs.getById(financialId),UserFinancialDto.class));
    }
    @Override
//    @RedisAnnotation
    public Result<UserFinancialDto> queryByGoldId(Integer goldId,Integer userId) {
        UserFinancial userFinancial = ufs.getOne(new QueryWrapper<UserFinancial>().eq("user_id", userId).eq("gold_id", goldId));
        return Result.success(BeanUtil.copyProperties(userFinancial,UserFinancialDto.class));
    }


    @RedisAnnotation
    @ApiOperation("分页查询持仓记录")
    @Override
    public Result<PageResult<UserFinancialDto>> financialQuery(OrderPageVo orderPageVo) {
        return Result.success( ufs.financialQuery(orderPageVo));
    }
//    @GlobalTransactional
    @RedisAnnotation(read = false,refresh = "com.moneywelcome.orderassembly.apiImpl.FinancialApiImpl.financialQuery")
    @ApiOperation("生成持仓记录")
    @Override
    public Result<UserFinancialDto> saveFinancial(UserFinancialVo userFinancialVo) {
        log.info("生成持仓记录,{}",userFinancialVo);
         return Result.success(ufs.create(userFinancialVo));
    }
//    @GlobalTransactional
    @ApiOperation("更新份额")
    /**
     * 购买赎回时更新份额
     * @param financialId
     * @param shares
     * @return
     */
    @Override
    @RedisAnnotation(read = false,refresh = "com.moneywelcome.orderassembly.apiImpl.FinancialApiImpl.financialQuery")
    public Result<UserFinancialDto> updateShares(Long financialId, Double shares) {
        log.info("持仓{}更新份额{}",financialId,shares);
        return Result.success(ufs.updateShares(financialId,shares));
    }
//    @GlobalTransactional
    @ApiOperation("更新收益")
    /**
     * 每日收益更新
     * @param financialId
     * @param revenue
     * @return
     */
    @Override
    @RedisAnnotation(read = false,refresh = "com.moneywelcome.orderassembly.apiImpl.FinancialApiImpl.financialQuery")
    public Result updateRevenue() throws IOException {
        log.info("持仓更新收益");
         ufs.updateRevenue();
        return Result.success();
    }

    /**
     * 生成每日收益
     * @param profitDayVo
     * @return
     */
    @Override
//    @GlobalTransactional
    @RedisAnnotation(read = false,refresh = "com.moneywelcome.orderassembly.apiImpl.FinancialApiImpl.queryById")
    public Result<ProfitDayDto> saveProfit(ProfitDayVo profitDayVo) {
        log.info("更新每日收益{}",profitDayVo);
        return Result.success(pds.create(profitDayVo));
    }
}
